from django.shortcuts import render
from django.views.generic import View
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q

from apps.courses.models import Course, Video
from apps.operations.models import UserFavorite,UserCourse
from apps.users.utils.mixin import LoginRequiredMiXin


class CoursePageView(View):
    """
    课程分页
    """
    def get(self, request):
        # 课程
        courses = Course.objects.all()
        # 热门课程只显示3个
        coursesHot = courses.order_by("-clickNum")[:3]
        # 全局搜索关键词
        keywords = request.GET.get("keywords", "")
        if keywords:
            courses = courses.filter(Q(name__icontains=keywords)|Q(desc__icontains=keywords)|Q(detail__contains=keywords))
        # 分页条件(最新，最热门，参与人数)
        sort = request.GET.get("sort","")
        if sort:
            courses = courses.order_by("-"+sort)
        # 对公开课进行分页
        try:
            page = request.GET.get("page", 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(courses, 5, request=request)
        coursePage = p.page(page)
        return render(request, "indexForCoursePage.html", {"sort": sort, "pageObj": coursePage, "coursesHot": coursesHot})


class CourseDetailView(LoginRequiredMiXin,View):
    """
    公开课-详情
    """
    def get(self,request,courseId):
        # 公开课
        course = Course.objects.get(id=int(courseId))
        # 修改点击数
        course.clickNum += 1
        course.save()
        # 获取相关的课程
        tag = course.tag
        if tag:
            coursesRelate = Course.objects.exclude(id=course.id).filter(tag__icontains=tag)[:2]
        # 是否收藏机构，课程
        hasFavCourse = False
        hasFavOrg = False
        if UserFavorite.objects.filter(user=request.user,favId=course.id,favType=3):
            hasFavCourse = True
        if UserFavorite.objects.filter(user=request.user,favId=course.org.id,favType=1):
            hasFavOrg = True
        return render(request,"courseDetail.html",{"course":course,
           "coursesRelate":coursesRelate,"hasFavCourse":hasFavCourse,"hasFavOrg":hasFavOrg})


class CourseVideoView(LoginRequiredMiXin,View):
    """
    公开课-章节
    """
    def get(self,request,courseId):
        # 课程
        course = Course.objects.get(id=courseId)
        course.students += 1
        course.save()
        # 关联用户和课程
        userCourses = UserCourse.objects.filter(user=request.user,course=course)
        if not userCourses:
            userCourse = UserCourse(user=request.user,course=course)
            userCourse.save()
        # 学过该课程的用户还学过其他课程
        userCoursesByCourse = UserCourse.objects.filter(course=course)
        users = [userCourse.user for userCourse in userCoursesByCourse]
        userCoursesByUsers = UserCourse.objects.filter(user__in=users)
        courseIds = [userCourse.course.id for userCourse in userCoursesByUsers]
        otherCourses = Course.objects.filter(id__in=courseIds).order_by("-clickNum")[:5]
        return render(request,"courseVideo.html",{"active1":"video",
              "course":course,"otherCourses":otherCourses})



class CourseCommentView(LoginRequiredMiXin,View):
    """
    公开课-评论
    """
    def get(self,request,courseId):
        # 课程
        course = Course.objects.get(id=courseId)
        # 学过该课程的用户还学过其他课程
        userCoursesByCourse = UserCourse.objects.filter(course=course)
        users = [userCourse.user for userCourse in userCoursesByCourse]
        userCoursesByUsers = UserCourse.objects.filter(user__in=users)
        courseIds = [userCourse.course.id for userCourse in userCoursesByUsers]
        otherCourses = Course.objects.filter(id__in=courseIds).order_by("-clickNum")[:5]
        return render(request,"courseComment.html",{"active1":"comment",
                "course":course,"otherCourses":otherCourses})



class PlayVideoView(LoginRequiredMiXin,View):
    """
    视频播放
    """
    def get(self,request,videoId):
        # 视频
        video = Video.objects.get(id=int(videoId))
        # 课程
        course = video.lesson.course
        # 关联用户和课程
        userCourses = UserCourse.objects.filter(user=request.user,course=course)
        if not userCourses:
            userCourse = UserCourse(user=request.user,course=course)
            userCourse.save()
        # 学过该课程的用户还学过其他课程
        userCoursesByCourse = UserCourse.objects.filter(course=course)
        users = [userCourse.user for userCourse in userCoursesByCourse]
        userCoursesByUsers = UserCourse.objects.filter(user__in=users)
        courseIds = [userCourse.course.id for userCourse in userCoursesByUsers]
        otherCourses = Course.objects.filter(id__in=courseIds).order_by("-clickNum")[:5]
        return render(request,"coursePlay.html",{"active1":"video",
              "course":course,"otherCourses":otherCourses,"video":video})


